package com.example.plat.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.example.plat.enity.Activity;
import com.example.plat.enity.Schedule;
import com.example.plat.enity.dto.ActivityCardDto;
import com.example.plat.enity.dto.ScheduleCardDto;
import com.example.plat.enity.dto.ScheduleDetailDto;
import com.example.plat.exception.ScheduleException;
import com.example.plat.service.PlaceService;
import com.example.plat.service.ScheduleService;
import com.example.plat.util.TimeFormateUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.modelmapper.ModelMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.util.List;
import java.util.stream.Collectors;

@Api(description = "活动日程管理")
@RestController
@RequestMapping("/schedule")
public class ScheduleComtroller {

    @Autowired
    private ModelMapper mapper;

    @Autowired
    private ScheduleService service;

    @Autowired
    private PlaceService placeService;

    @ApiOperation("获取发布活动卡片")
    @GetMapping("/my/{userName}")
    public IPage<ScheduleCardDto> getMyScheduleCard(String userName,
                                                    @RequestParam(defaultValue = "1") Integer pageNum,
                                                    @RequestParam(defaultValue = "10") Integer pageSize) {
        IPage<Activity> page = service.getMyScheduleCard(userName, pageNum, pageSize);

        return page.convert(u -> {
            ScheduleCardDto v = new ScheduleCardDto();
            BeanUtils.copyProperties(u, v);
            return v;
        });
    }

    @ApiOperation("获取参与活动卡片")
    @GetMapping("/{userId}")
    public ResponseEntity<List<ScheduleCardDto>> getJoinScheduleCard(@ApiParam("用户ID") @PathVariable Long userId,
                                                                     @RequestParam(defaultValue = "1") Integer pageNum,
                                                                     @RequestParam(defaultValue = "10") Integer pageSize) {
        List<Activity> page = service.getJoinScheduleCard(userId, pageNum, pageSize);

        List<ScheduleCardDto> dtos = page.stream().map(u ->
            mapper.map(u, ScheduleCardDto.class)
        ).collect(Collectors.toList());
        return ResponseEntity.ok(dtos);
    }

    @ApiOperation("根据活动ID获取活动卡片")
    @GetMapping("/one/{actId}")
    public ResponseEntity<ScheduleCardDto> getOneCard(@ApiParam("活动ID")@PathVariable Long actId){
        Activity card = service.getOneCard(actId);

        return ResponseEntity.ok(mapper.map(card,ScheduleCardDto.class));
    }

    @ApiOperation("获取活动日程")
    @GetMapping
    public ResponseEntity<List<ScheduleDetailDto>> getScheduleDetail(@ApiParam("活动ID")@RequestParam Long activityId) {
        List<Schedule> detail = service.getScheduleDetail(activityId);
        List<ScheduleDetailDto> dtos = detail.stream().map(u -> {
            ScheduleDetailDto v = mapper.map(u, ScheduleDetailDto.class);
            try {
                v.setTime(TimeFormateUtil.stampToTime(u.getTimeT()));
            } catch (ParseException e) {
                throw new RuntimeException(e);
            }
            return v;
        }).collect(Collectors.toList());
        return ResponseEntity.ok(dtos);
    }

    @ApiOperation("创建活动日程")
    @PostMapping("/{activityId}")
    public ResponseEntity<String> addSchedule(@ApiParam("活动ID") @PathVariable Long activityId,
                                              @RequestBody List<ScheduleDetailDto> dtos) throws ScheduleException {
        List<Schedule> schedules = dtos.stream().map(u -> {
            Schedule v = mapper.map(u, Schedule.class);
            v.setActId(activityId);
            return v;
        }).collect(Collectors.toList());

        service.addSchedule(schedules);

        return ResponseEntity.ok("success!");
    }

    @ApiOperation("修改日程")
    @PutMapping("/{activityId}")
    public ResponseEntity<String> updateSchedule(@ApiParam("活动ID") @PathVariable Long activityId,
                                                 @RequestBody List<ScheduleDetailDto> dtos) throws ScheduleException {
        List<Schedule> schedules = dtos.stream().map(u -> {
            Schedule v = mapper.map(u, Schedule.class);
            v.setActId(activityId);
            try {
                v.setTimeT(TimeFormateUtil.timeToStamp(u.getTime()));
            } catch (ParseException e) {
                throw new RuntimeException(e);
            }
            return v;
        }).collect(Collectors.toList());

        service.updateSchedule(schedules);

        return ResponseEntity.ok("success");
    }

}
